﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper;
using RH.Domain;
using RH.Web.ViewModels;

namespace RH.Web.Utility
{
    public static class UserInfo
    {
        public static bool IsAdmin
        {
            get
            {
                if (SessionCache.CurrentUser != null && SessionCache.CurrentUser.GroupID == ConfigReader.UserGroupIDOfAdmin)
                    return true;
                return false;
            }
        }
        public static bool IsSiteUser
        {
            get
            {
                if (SessionCache.CurrentUser != null && SessionCache.CurrentUser.GroupID == ConfigReader.UserGroupIDOfSiteUser)
                    return true;
                return false;
            }
        }
        public static bool IsCompanyUser
        {
            get
            {
                if (SessionCache.CurrentUser != null && SessionCache.CurrentUser.Company != null)
                    return true;
                return false;
            }
        }
        public static bool IsCompanyAdmin
        {
            get
            {
                if (SessionCache.CurrentUser != null && SessionCache.CurrentUser.Company != null && SessionCache.CurrentUser.GroupID == ConfigReader.UserGroupIDOfCompanyAdmin)
                    return true;
                return false;
            }
        }
        public static bool IsCompanyEmployeeUser
        {
            get
            {
                if (SessionCache.CurrentUser != null && SessionCache.CurrentUser.Company != null && SessionCache.CurrentUser.GroupID == ConfigReader.UserGroupIDOfCompanyUser)
                    return true;
                return false;
            }
        }

        public static String GetUserType()
        {
            if (SessionCache.CurrentUser != null)
            {                
                GroupViewModel group = ApplicationCache.UserGroups.FirstOrDefault(P => P.ID == SessionCache.CurrentUser.GroupID);
                return group.Name;
            }
            return "Not Logged In";
        }

        #region Utility Methods
        /// <summary>
        /// Populates the Session for an previously Authenticated User
        /// </summary>
        public static void ReLoginUser()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated && SessionCache.CurrentUser == null)
            {
                String userName = HttpContext.Current.User.Identity.Name;
                AppEntities context = new AppEntities();
                User user = context.Users.FirstOrDefault(U => String.Compare(U.UserName, userName, true) == 0);
                if (user != null)
                {
                    ///Load Group and Group Permissions                                
                    //user.UserGroup = groupDAO.Get(user.GroupCode);                
                    UserViewModel userViewModel = null;
                    userViewModel = Mapper.Map<User, UserViewModel>(user, userViewModel);

                    ///Load Compnay Information, if Exists
                    LoadCompanyInfo(userViewModel, user, context);
                    SessionCache.CurrentUser = userViewModel;
                }
            }
        }
        /// <summary>
        /// Loads Company Information to the UserViewModel Object if user is a companys User
        /// </summary>
        /// <param name="userViewModel"></param>
        /// <param name="user"></param>
        /// <param name="context"></param>
        public static void LoadCompanyInfo(UserViewModel userViewModel, User user, RH.Domain.AppEntities context)
        {
            if (user.ContactID.HasValue)
            {
                Contact contact = context.Contacts.Include("Company.Products").Include("Company.Contacts").FirstOrDefault(P => P.ID == user.ContactID);
                if (contact != null && contact.Company != null)
                {
                    userViewModel.Company = Mapper.Map<Company, CompanyViewModel>(contact.Company);
                    RefreshUserInfo(userViewModel, contact.Company);
                }
            }
        }

        /// <summary>
        /// Reloads Company Information from Database
        /// </summary>
        /// <param name="userViewModel"></param>
        /// <param name="company"></param>
        private static void RefreshUserInfo(UserViewModel userViewModel, Company company)
        {
            CompanyInfo info = new CompanyInfo();
            info.ProjectCount = company.Products.Count(P => !P.IsDeleted);
            //FIXME: Need a way to calculate profile incompleteness.
            info.IsIncompleteProfile = false;
            //FIXME: Need to update when Leads module is done
            info.UnreadLeadCount = 8;
            info.ContactCount = company.Contacts.Count(P => !P.IsDeleted);

            userViewModel.CompanyInfo = info;
        }
        public static void RefreshUserInfo(EntityRepository repository)
        {
            if (SessionCache.CurrentUser != null)
            {
                UserViewModel user = SessionCache.CurrentUser;
                if (UserInfo.IsCompanyUser)
                {
                    Company company = repository.GetCompanyByID(user.Company.ID);
                    RefreshUserInfo(user, company);
                }
                SessionCache.CurrentUser = user;
            }
        }
        #endregion
    }

    #region User Menu Enumerations

    public enum CompanyMenu
    {
        Dashboard,
        Profile,
        Contacts,
        Projects,
        ProjectLeads,
        ChangePassword,
        None
    }
    public enum IndividualMenu
    {
        Dashboard,
        Profile,
        Projects,
        Leads,
        None
    }
    public enum SiteUserMenu
    {
        Dashboard,
        Profile,
        Favorites,
        SavedSearch,
        Leads,        
        None
    }    
    public enum AdminMenu
    {
        Dashboard,
        ManageLookupData,
        ManagePackage,
        ManageContents,
        ManageCategory,
        SearchConfig,
        BackupDatabase,
        Settings,
        None
    }

    #endregion User Menu Enumerations
}